import { http } from "@/utils/http";

interface OrderResponse {
  code: number;
  message: string;
  data?: any;
  rows?: any[];
  total?: number;
}

/** 订单状态枚举 */
export enum OrderStatus {
  PENDING_PAYMENT = 1, // 待付款
  PAID = 2, // 已付款
  PENDING_SHIPMENT = 3, // 待发货
  SHIPPED = 4, // 已发货
  COMPLETED = 5, // 已完成
  CANCELLED = 6 // 已取消
}

/** 获取订单列表 */
export const getOrdersList = (params?: object) => {
  return http.request<OrderResponse>("get", "/api/order-management/orders", {
    params
  });
};

/** 获取订单详情 */
export const getOrderDetail = (orderId: number) => {
  return http.request<OrderResponse>("get", `/api/order-management/orders/${orderId}`);
};

/** 更新订单状态 */
export const updateOrderStatus = (orderId: number, status: number, remark?: string) => {
  return http.request<OrderResponse>("put", `/api/order-management/orders/${orderId}/status`, {
    data: { status, remark }
  });
};

/** 批量更新订单状态 */
export const batchUpdateOrderStatus = (orderIds: number[], status: number, remark?: string) => {
  return http.request<OrderResponse>("put", "/order-management/orders/batch-status", {
    data: { orderIds, status, remark }
  });
};

/** 更新订单备注 */
export const updateOrderRemark = (orderId: number, remark: string) => {
  return http.request<OrderResponse>("put", `/order-management/orders/${orderId}/remark`, {
    data: { remark }
  });
};

/** 订单发货 */
export const shipOrder = (orderId: number, shippingData: any) => {
  return http.request<OrderResponse>("post", `/order-management/orders/${orderId}/ship`, {
    data: shippingData
  });
};

/** 批量发货 */
export const batchShipOrders = (orders: any[]) => {
  return http.request<OrderResponse>("post", "/order-management/orders/batch-ship", {
    data: { orders }
  });
};

/** 获取订单日志 */
export const getOrderLogs = (orderId: number) => {
  return http.request<OrderResponse>("get", `/order-management/orders/${orderId}/logs`);
};

/** 导出订单 */
export const exportOrders = (params?: object) => {
  return http.request<OrderResponse>("get", "/order-management/orders/export", {
    params,
    responseType: "blob"
  });
};

/** 获取订单统计 */
export const getOrderStatistics = (params?: object) => {
  return http.request<OrderResponse>("get", "/order-management/orders/statistics", { params });
};

/** 取消订单 */
export const cancelOrder = (orderId: number, reason: string) => {
  return http.request<OrderResponse>("post", `/order-management/orders/${orderId}/cancel`, {
    data: { reason }
  });
};

/** 获取物流信息 */
export const getLogisticsInfo = (orderId: number) => {
  return http.request<OrderResponse>("get", `/order-management/orders/${orderId}/logistics`);
};
